package com.ph.irp.base.bom.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.bom.service.BOMManageService;
import com.ph.irp.dto.BOMBillDetailDTO;
import com.ph.irp.dto.BOMGroupDTO;
import com.ph.irp.dto.BomBillDTO;
import com.ph.irp.dto.PageDTO;
import com.ph.irp.model.PBaseBombill;
import com.ph.irp.model.PBaseBombilldetail;
import com.ph.irp.model.PBaseBomgroup;
import com.ph.util.DateUtil;
import com.ph.util.StringUtil;
import com.ph.util.pagination.PageUtil;
import com.ph.util.pagination.ShowPageList;

public class BOMManageAction extends ActionSupport implements ShowPageList {
	private String FORWARD = null;
	private HttpServletRequest request;
	private String alertMessage;
	private BOMManageService bomManageService;
	private List<BOMGroupDTO> bomGroupDTOList;
	private List<BomBillDTO> bomBillDTOList;
	private List<BomBillDTO> tempBomBillDTOList;
	private PageDTO page;
	private String cpage;

	private static final String BOMNUMSUFFIX = "BOM";

	private String bomGroupCode;
	private String bomGroupName;
	private String bomGroupParentID;
	private String bomGroupID;

	private PBaseBomgroup editedBOMGroup;
	private BomBillDTO editedBOMBill;
	private String editBOMBillID;
	private List<BOMBillDetailDTO> editedBOMBillDetailList;

	private String billMakerName;
	private String billMakeDate;
	private String billEditerName;
	private String billEditerDate;
	private String billAuditerName;
	private String billAuditDate;
	private String bomBillCode;
	private String state;
	private String auditState;
	private String materialId;
	private String unitName;
	private String number;
	private String fee;
	private String memo;

	private String[] bomDetailRow;

	public List<BomBillDTO> getTempBomBillDTOList() {
		return tempBomBillDTOList;
	}

	public void setTempBomBillDTOList(List<BomBillDTO> tempBomBillDTOList) {
		this.tempBomBillDTOList = tempBomBillDTOList;
	}

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	public String getBillEditerName() {
		return billEditerName;
	}

	public void setBillEditerName(String billEditerName) {
		this.billEditerName = billEditerName;
	}

	public String getBillEditerDate() {
		return billEditerDate;
	}

	public void setBillEditerDate(String billEditerDate) {
		this.billEditerDate = billEditerDate;
	}

	public List<BOMBillDetailDTO> getEditedBOMBillDetailList() {
		return editedBOMBillDetailList;
	}

	public void setEditedBOMBillDetailList(
			List<BOMBillDetailDTO> editedBOMBillDetailList) {
		this.editedBOMBillDetailList = editedBOMBillDetailList;
	}

	public BomBillDTO getEditedBOMBill() {
		return editedBOMBill;
	}

	public void setEditedBOMBill(BomBillDTO editedBOMBill) {
		this.editedBOMBill = editedBOMBill;
	}

	public String getEditBOMBillID() {
		return editBOMBillID;
	}

	public void setEditBOMBillID(String editBOMBillID) {
		this.editBOMBillID = editBOMBillID;
	}

	public String[] getBomDetailRow() {
		return bomDetailRow;
	}

	public void setBomDetailRow(String[] bomDetailRow) {
		this.bomDetailRow = bomDetailRow;
	}

	public String getBillAuditerName() {
		return billAuditerName;
	}

	public void setBillAuditerName(String billAuditerName) {
		this.billAuditerName = billAuditerName;
	}

	public String getBillAuditDate() {
		return billAuditDate;
	}

	public void setBillAuditDate(String billAuditDate) {
		this.billAuditDate = billAuditDate;
	}

	public String getUnitName() {
		return unitName;
	}

	public void setUnitName(String unitName) {
		this.unitName = unitName;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getFee() {
		return fee;
	}

	public void setFee(String fee) {
		this.fee = fee;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getMaterialId() {
		return materialId;
	}

	public void setMaterialId(String materialId) {
		this.materialId = materialId;
	}

	public String getBillMakerName() {
		return billMakerName;
	}

	public void setBillMakerName(String billMakerName) {
		this.billMakerName = billMakerName;
	}

	public String getBillMakeDate() {
		return billMakeDate;
	}

	public void setBillMakeDate(String billMakeDate) {
		this.billMakeDate = billMakeDate;
	}

	public String getAlertMessage() {
		return alertMessage;
	}

	public void setAlertMessage(String alertMessage) {
		this.alertMessage = alertMessage;
	}

	public String getBomGroupID() {
		return bomGroupID;
	}

	public void setBomGroupID(String bomGroupID) {
		this.bomGroupID = bomGroupID;
	}

	public String getBomBillCode() {
		return bomBillCode;
	}

	public void setBomBillCode(String bomBillCode) {
		this.bomBillCode = bomBillCode;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getAuditState() {
		return auditState;
	}

	public void setAuditState(String auditState) {
		this.auditState = auditState;
	}

	public List<BomBillDTO> getBomBillDTOList() {
		return bomBillDTOList;
	}

	public void setBomBillDTOList(List<BomBillDTO> bomBillDTOList) {
		this.bomBillDTOList = bomBillDTOList;
	}

	public PBaseBomgroup getEditedBOMGroup() {
		return editedBOMGroup;
	}

	public void setEditedBOMGroup(PBaseBomgroup editedBOMGroup) {
		this.editedBOMGroup = editedBOMGroup;
	}

	public String getBomGroupParentID() {
		return bomGroupParentID;
	}

	public void setBomGroupParentID(String bomGroupParentID) {
		this.bomGroupParentID = bomGroupParentID;
	}

	public String getBomGroupCode() {
		return bomGroupCode;
	}

	public void setBomGroupCode(String bomGroupCode) {
		this.bomGroupCode = bomGroupCode;
	}

	public String getBomGroupName() {
		return bomGroupName;
	}

	public void setBomGroupName(String bomGroupName) {
		this.bomGroupName = bomGroupName;
	}

	public void setBomManageService(BOMManageService bomManageService) {
		this.bomManageService = bomManageService;
	}

	public List<BOMGroupDTO> getBomGroupDTOList() {
		return bomGroupDTOList;
	}

	public void setBomGroupDTOList(List<BOMGroupDTO> bomGroupDTOList) {
		this.bomGroupDTOList = bomGroupDTOList;
	}

	public String showBOMGroupList() throws Throwable {
		bomGroupDTOList = bomManageService.getAllBOMGroup();

		if (bomGroupDTOList != null && bomGroupDTOList.size() != 0) {
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}

		return FORWARD;

	}

	public String getBOMGroup() throws Throwable {
		bomGroupDTOList = bomManageService.getBOMGroupByCodeAndName(
				bomGroupCode, bomGroupName);

		if (bomGroupDTOList != null && bomGroupDTOList.size() != 0) {
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}

		return FORWARD;
	}

	public String getBOMBill() throws Throwable {
		tempBomBillDTOList = bomManageService.getBOMBillByStateAndCode(
				bomBillCode, state, auditState, bomGroupID);

		if (tempBomBillDTOList != null) {
			if (tempBomBillDTOList.size() != 0) {
				cpage = "1";
				page = PageUtil.generatePageDTO(tempBomBillDTOList,
						"showBOMPageList.action", cpage);
				bomBillDTOList = page.getResList();
			} else {
				bomBillDTOList = tempBomBillDTOList;
			}
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}

		return FORWARD;
	}

	public String useBomBill() throws Throwable {
		request = ServletActionContext.getRequest();
		String useBomBillID = (String) request.getParameter("useBomBillID");

		if (useBomBillID != null && !useBomBillID.equals("")) {
			if (bomManageService.getAuditStateByBOMBillID(useBomBillID) != 0) {

				if (bomManageService.getStateByBOMBillID(useBomBillID) == 0) {
					bomManageService.setBomBillUsed(useBomBillID);

					FORWARD = "success";
				} else {
					setAlertMessage("该BOM单已被使用");
					FORWARD = "input";
				}

			} else {
				setAlertMessage("不能对未审核的BOM单进行使用");
				FORWARD = "input";
			}

		}

		return FORWARD;
	}

	public String antiUseBomBill() throws Throwable {
		request = ServletActionContext.getRequest();
		String useBomBillID = (String) request.getParameter("useBomBillID");

		if (useBomBillID != null && !useBomBillID.equals("")) {
			if (bomManageService.getAuditStateByBOMBillID(useBomBillID) != 0) {

				if (bomManageService.getStateByBOMBillID(useBomBillID) != 0) {
					bomManageService.setBomBillAntiUsed(useBomBillID);

					FORWARD = "success";
				} else {
					setAlertMessage("该BOM单还未被使用");
					FORWARD = "input";
				}

			} else {
				setAlertMessage("不能对未审核的BOM单进行反使用");
				FORWARD = "input";
			}

		}

		return FORWARD;
	}

	public String auditBomBill() throws Throwable {
		request = ServletActionContext.getRequest();
		String useBomBillID = (String) request.getParameter("useBomBillID");

		billAuditerName = (String) request.getSession()
				.getAttribute("userName");
		billAuditDate = DateUtil.getCurrentDateString();
		if (useBomBillID != null && !useBomBillID.equals("")) {
			if (bomManageService.getAuditStateByBOMBillID(useBomBillID) == 0) {
				bomManageService.setBomBillAudited(useBomBillID,
						billAuditerName, billAuditDate);

				FORWARD = "success";
			} else {
				setAlertMessage("该BOM单已被审核");
				FORWARD = "input";
			}

		}

		return FORWARD;
	}

	public String antiAuditBomBill() throws Throwable {
		request = ServletActionContext.getRequest();
		String useBomBillID = (String) request.getParameter("useBomBillID");

		if (useBomBillID != null && !useBomBillID.equals("")) {
			if (bomManageService.getAuditStateByBOMBillID(useBomBillID) != 0) {
				bomManageService.setBomBillAntiAudited(useBomBillID);

				FORWARD = "success";
			} else {
				setAlertMessage("该BOM单还未被审核");
				FORWARD = "input";
			}

		}

		return FORWARD;
	}

	public String showAddBOMGroup() throws Throwable {
		return showBOMGroupList();
	}

	public String addBOMGroup() throws Throwable {
		bomManageService.addBOMGroup(bomGroupCode, bomGroupName,
				bomGroupParentID);

		FORWARD = "success";

		return FORWARD;
	}

	public String showEditBOMGroup() throws Throwable {
		request = ServletActionContext.getRequest();
		String editBOMGroupID = (String) request.getParameter("editBOMGroupID");

		editedBOMGroup = bomManageService
				.getBOMGroupByBOMGroupId(editBOMGroupID);

		if (editedBOMGroup != null) {
			request.setAttribute("bomList", bomManageService
					.getAllBOMGroupExcludeBOMGroupID(editBOMGroupID));
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}

		return FORWARD;
	}

	public String editBOMGroup() throws Throwable {
		bomManageService.editBOMGroup(editedBOMGroup);

		FORWARD = "success";
		return FORWARD;
	}

	public String deleteBOMGroup() throws Throwable {
		request = ServletActionContext.getRequest();
		String deletedBOMGroupID = (String) request
				.getParameter("deletedBOMGroupID");

		if (deletedBOMGroupID != null && !deletedBOMGroupID.equals("")) {
			bomManageService.deleteBOMGroup(StringUtil
					.removeLastComma(deletedBOMGroupID));
		}

		FORWARD = "success";
		return FORWARD;
	}

	public String showBOMBill() throws Throwable {
		request = ServletActionContext.getRequest();
		bomGroupID = (String) request.getParameter("bomGroupID");

		// bomGroupDTOList = bomManageService.getAllBOMGroup();
		tempBomBillDTOList = bomManageService
				.getBOMBillByBOMGroupID(bomGroupID);
		if (tempBomBillDTOList != null) {
			if (tempBomBillDTOList.size() != 0) {
				cpage = "1";
				page = PageUtil.generatePageDTO(tempBomBillDTOList,
						"showBOMPageList.action", cpage);
				bomBillDTOList = page.getResList();
			} else {
				bomBillDTOList = tempBomBillDTOList;
			}
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}

		return FORWARD;
	}

	public String showAddBOMBill() throws Throwable {
		request = ServletActionContext.getRequest();

		bomBillCode = BOMNUMSUFFIX
				+ StringUtil.generateSequenceNumber(bomManageService
						.getNewestBomBillCode(), BOMNUMSUFFIX);

		bomGroupName = bomManageService.getBOMGroupByBOMGroupId(bomGroupID)
				.getBomgroupName();
		billMakerName = (String) request.getSession().getAttribute("userName");
		billMakeDate = DateUtil.getCurrentDateString();

		if (bomGroupName != null && billMakerName != null
				&& bomBillCode != null && billMakeDate != null) {
			editedBOMBill = new BomBillDTO();
			editedBOMBill.setBomBillCode(bomBillCode);
			editedBOMBill.setBomGroupId(Integer.parseInt(bomGroupID));
			editedBOMBill.setBomGroupName(bomGroupName);
			editedBOMBill.setBillMakerName(billMakerName);
			editedBOMBill.setBillMakeDate(billMakeDate);
			editedBOMBill.setState("未使用");
			editedBOMBill.setNumber(Float.parseFloat("1.00"));
			editedBOMBill.setFee(Float.parseFloat("0.00"));

			editedBOMBillDetailList = null;
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}

		return FORWARD;
	}

	public String showEditBOMBill() throws Throwable {
		request = ServletActionContext.getRequest();
		// editBOMBillID
		if (bomManageService.isAuditedBOMBill(editBOMBillID)) {
			setAlertMessage("不能修改已审核的BOM单");
			FORWARD = "input";
		} else {
			editedBOMBill = bomManageService
					.getBOMBillByBOMBillID(editBOMBillID);
			editedBOMBill.setBillEditerName((String) request.getSession()
					.getAttribute("userName"));
			editedBOMBill.setBillEditerDate(DateUtil.getCurrentDateString());

			editedBOMBillDetailList = bomManageService
					.getBOMBillDetailListByBOMBillID(editBOMBillID);
			FORWARD = "success";
		}
		return FORWARD;
	}

	public BOMManageService getBomManageService() {
		return bomManageService;
	}

	public String addBOMBill() throws Throwable {
		request = ServletActionContext.getRequest();
		PBaseBombill bomBill = new PBaseBombill();
		bomBill.setBomBillCode(bomBillCode);
		bomBill.setBomGroupId(Integer.parseInt(bomGroupID));
		bomBill.setVerifyProjectMaterialId(Integer.parseInt(materialId));
		bomBill.setState(0);
		bomBill.setAuditState(0);
		// bomBill.setAttributeBasicType(assistantAttribute);
		bomBill.setUnit(unitName);
		bomBill.setNumber(Float.parseFloat(number));
		bomBill.setFee(Float.parseFloat(fee));
		bomBill.setMemo(memo);

		bomManageService.addBOMBill(bomBill);

		// 将表单明细加入
		int bomBillID = bomManageService.getBOMBillIdByBOMBillCode(bomBillCode);

		if (bomDetailRow != null) {
			for (String row : bomDetailRow) {
				String[] tempStr = row.split(",");
				// bomDetailRows[i].value =
				// tempMaterialId.value+","+tempMaterialAttribute.value+","
				// +tempStorageId.value+","+tempCost.value+","+tempLossPercent.value+","+tempMemo.value;

				PBaseBombilldetail bomBillDatail = new PBaseBombilldetail();
				bomBillDatail.setPBaseBombillId(bomBillID);
				bomBillDatail.setPBaseVerifyprojectMaterialId(Integer
						.parseInt(tempStr[0]));
				bomBillDatail.setPBaseVerifyprojectStorageId(Integer
						.parseInt(tempStr[2]));
				bomBillDatail.setUsedNumber(Double.parseDouble(tempStr[3]));
				bomBillDatail.setLossPercent(Integer.parseInt(tempStr[4]));
				bomBillDatail.setPBaseMeasureUnitId(Integer
						.parseInt(tempStr[5]));
				bomManageService.addBOMBillDetail(bomBillDatail);
			}
		}
		FORWARD = "success";

		return FORWARD;
	}

	public String editBOMBill() throws Throwable {
		request = ServletActionContext.getRequest();

		PBaseBombill bomBill = new PBaseBombill();
		bomBill.setBomBillId(Integer.parseInt(editBOMBillID));
		bomBill.setMemo(memo);
		bomBill.setFee(Float.parseFloat(fee));
		bomBill.setNumber(Float.parseFloat(number));
		bomBill.setUnit(unitName);
		bomBill.setVerifyProjectMaterialId(Integer.parseInt(materialId));

		bomManageService.editBOMBill(bomBill);

		// 将表单明细修改
		// 先删除后增加
		bomManageService.deleteBOMBillDetailByBOMBillID(editBOMBillID);

		if (bomDetailRow != null) {
			for (String row : bomDetailRow) {
				String[] tempStr = row.split(",");
				// bomDetailRows[i].value =
				// tempMaterialId.value+","+tempMaterialAttribute.value+","
				// +tempStorageId.value+","+tempCost.value+","+tempLossPercent.value+","+tempMemo.value;

				PBaseBombilldetail bomBillDatail = new PBaseBombilldetail();
				bomBillDatail
						.setPBaseBombillId(Integer.parseInt(editBOMBillID));
				bomBillDatail.setPBaseVerifyprojectMaterialId(Integer
						.parseInt(tempStr[0]));
				bomBillDatail.setPBaseVerifyprojectStorageId(Integer
						.parseInt(tempStr[2]));
				bomBillDatail.setUsedNumber(Double.parseDouble(tempStr[3]));
				bomBillDatail.setLossPercent(Integer.parseInt(tempStr[4]));
				bomBillDatail.setPBaseMeasureUnitId(Integer
						.parseInt(tempStr[5]));
				bomManageService.addBOMBillDetail(bomBillDatail);
			}
		}
		FORWARD = "success";

		return FORWARD;
	}

	public String deleteBOMBill() throws Throwable {
		request = ServletActionContext.getRequest();
		String deletedBOMBillID = (String) request
				.getParameter("deletedBOMBillID");
		String deleteBOMBillIDs = StringUtil.removeLastComma(deletedBOMBillID);
		boolean flag = false;
		if (deletedBOMBillID != null && !deletedBOMBillID.equals("")) {
			String[] deleteIDs = deleteBOMBillIDs.split(",");
			for (String deleteBOMBillID : deleteIDs) {
				if (bomManageService.isAuditedBOMBill(deleteBOMBillID)) {
					flag = false;
					break;
				} else {
					flag = true;
				}
			}

			if (flag) {
				for (String deleteBOMBillID : deleteIDs) {
					bomManageService
							.deleteBOMBillDetailByBOMBillID(deleteBOMBillID);
				}
				bomManageService.deleteBOMBill(deleteBOMBillIDs);
				FORWARD = "success";
			} else {
				setAlertMessage("不能删除已审核的BOM单");
				FORWARD = "input";
			}
		}

		return FORWARD;
	}

	public String copyBOMBill() throws Throwable {
		request = ServletActionContext.getRequest();
		String useBomBillID = (String) request.getParameter("useBomBillID");
		bomBillCode = BOMNUMSUFFIX
				+ StringUtil.generateSequenceNumber(bomManageService
						.getNewestBomBillCode(), BOMNUMSUFFIX);
		if (useBomBillID != null && !useBomBillID.equals("")) {
			editedBOMBill = bomManageService
					.getBOMBillByBOMBillID(useBomBillID);
			editedBOMBill.setBillMakerName((String) request.getSession()
					.getAttribute("userName"));
			editedBOMBill.setBillMakeDate(DateUtil.getCurrentDateString());
			editedBOMBill.setBomBillCode(bomBillCode);
			editedBOMBill.setBillAuditerName("");
			editedBOMBill.setBillAuditDate(null);

			editedBOMBillDetailList = bomManageService
					.getBOMBillDetailListByBOMBillID(useBomBillID);
			FORWARD = "success";
		} else {
			setAlertMessage("该BOM单不能被复制");
			FORWARD = "input";
		}

		return FORWARD;
	}

	public String showPageList() throws Throwable {
		if (tempBomBillDTOList != null) {
			page = PageUtil.generatePageDTO(tempBomBillDTOList,
					"showBOMPageList.action", cpage);
			bomBillDTOList = page.getResList();
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}
		return FORWARD;
	}

	public String showBOMBillDetail() throws Throwable {
		request = ServletActionContext.getRequest();
		// editBOMBillID
		String bomBillID = request.getParameter("bomBillID");
		editedBOMBill = bomManageService.getBOMBillByBOMBillID(bomBillID);
		editedBOMBill.setBillEditerName((String) request.getSession()
				.getAttribute("userName"));
		editedBOMBill.setBillEditerDate(DateUtil.getCurrentDateString());

		editedBOMBillDetailList = bomManageService
				.getBOMBillDetailListByBOMBillID(bomBillID);
		FORWARD = "success";

		return FORWARD;
	}

}
